
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.IO;

namespace ComBrowser
{
	public partial class OptionsViewController : UIViewController
	{
		#region Constructors

		// The IntPtr and initWithCoder constructors are required for controllers that need 
		// to be able to be created from a xib rather than from managed code

		public OptionsViewController (IntPtr handle) : base(handle)
		{
			Initialize ();
		}

		[Export("initWithCoder:")]
		public OptionsViewController (NSCoder coder) : base(coder)
		{
			Initialize ();
		}

		public OptionsViewController () : base("OptionsViewController", null)
		{
			Initialize ();
		}

		MainViewController ctrlParent;
		public OptionsViewController (MainViewController CtrlParent)
		{
			this.ctrlParent = CtrlParent;
		}
		
		
		void Initialize ()
		{
		}
		
		#endregion
		
		int originalFont=0;
		int originalSize=0;
		int originalColor=0;
		
		public override void ViewDidLoad ()
		{
			SetGraphicsAndUI ();
			WireUpCommands ();
			base.ViewDidLoad ();
		}

		void SetGraphicsAndUI ()
		{
			UIImage bck = UIImage.FromFileUncached (Path.Combine(NSBundle.MainBundle.BundlePath,@"Images/LargeBackgroundWithCloseRightSideUp.png"));
			if (bck != null)
				imgBackground.Image = bck;
			
			MakeButtonsPretty ();
			
			//Cetner it
			float left = (UIScreen.MainScreen.ApplicationFrame.Width - this.View.Frame.Width) / 2;
			float top = (UIScreen.MainScreen.ApplicationFrame.Height - this.View.Frame.Height) / 2;
			this.View.Frame = new System.Drawing.RectangleF (left, top, this.View.Frame.Width, this.View.Frame.Height);
			
			//Set the picker
			segTextSize.SelectedSegment = Settings.TextSize;
			segFont.SelectedSegment = Settings.Font;
			segColor.SelectedSegment = Settings.ColorScheme;
			
			//RememberOldSettings
			originalSize = Settings.TextSize;
			originalFont = Settings.Font;
			originalColor = Settings.ColorScheme;
		}

		void WireUpCommands ()
		{
			segTextSize.ValueChanged += delegate { TextSizeChanged (); };
			segFont.ValueChanged += delegate { FontChanged (); };
			segColor.ValueChanged += delegate { ColorSchemeChanged (); };
			
			btnOk.TouchUpInside += delegate { ctrlParent.HideOptions (); };
			btnClose.TouchUpInside += delegate { HideOptionsAndRevertSettings (); };
		}

		void HideOptionsAndRevertSettings ()
		{
			Settings.TextSize = originalSize;
			Settings.Font = originalFont;
			Settings.ColorScheme = originalColor;
			ctrlParent.HideOptions ();
		}


		void TextSizeChanged ()
		{
			Settings.TextSize = segTextSize.SelectedSegment;	
		}

		void FontChanged ()
		{
			Settings.Font = segFont.SelectedSegment;
		}
		
		void ColorSchemeChanged ()
		{
			Settings.ColorScheme = segColor.SelectedSegment;
		}
		
		void MakeButtonsPretty ()
		{
			
			string blue = Path.Combine (NSBundle.MainBundle.BundlePath, @"Images/blueButton.png");
			string white = Path.Combine (NSBundle.MainBundle.BundlePath, @"Images/whiteButton.png");
			
			UIImage imgBlue = UIImage.FromFileUncached (blue);
			UIImage imgWhite = UIImage.FromFileUncached (white);
			
			if (imgBlue != null && imgWhite != null) {
				
				var newImage = imgWhite.StretchableImage (12, 0);
				var newPressedImage = imgBlue.StretchableImage (12, 0);
				
				btnOk.SetBackgroundImage (newImage, UIControlState.Normal);
				btnOk.SetBackgroundImage (newPressedImage, UIControlState.Highlighted);;
				
			}
		}
	}
}
